<template>
  <div class="drawerContent">
    <div style="position: absolute;left: 175px;top: 27px;font-size: 26px;">
      总重量为{{ totalNetWeight || 0 }}吨
    </div>
    <div class="searchContent fl">
      <div class="searchSimple fl">
        <div class="fl">
          <span style="font-size: 18px;">毛重时间</span>
          <!-- <el-date-picker
            v-model="searchTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleSearch"
          >
          </el-date-picker> -->
          <el-date-picker
            v-model="searchTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            @change="handleSearch"
          >
          </el-date-picker>
        </div>
        <div class="fr">
          <el-input
            v-model="searchStr"
            placeholder="请输入关键字 车牌号/发货方/收货方/货品/磅单号"
            :disabled="showDetailSearch"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              :disabled="showDetailSearch"
              @click="handleSearch"
            ></el-button>
          </el-input>
          <el-button
            :icon="showDetailSearch ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
            type="info"
            @click="handleDetailSearch"
          ></el-button>
        </div>
      </div>
      <div class="searchDetail fl" v-show="showDetailSearch">
        <el-form ref="form" :model="searchForm" label-width="80px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="货品">
                <!-- <el-input v-model="searchForm.payload" clearable></el-input> -->
                <el-select
                  v-model="searchForm.payload"
                  placeholder="请选择"
                  class="select_input"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in goodSpec"
                    :key="item.id_"
                    :label="item.payload_spec"
                    :value="item.payload_spec"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="发货方">
                <!-- <el-input v-model="searchForm.vender" clearable></el-input> -->
                <el-select
                  v-model="searchForm.vender"
                  placeholder="请选择"
                  class="select_input"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in proLines"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="收货方">
                <!-- <el-input v-model="searchForm.customer" clearable></el-input> -->
                <el-select
                  v-model="searchForm.customer"
                  placeholder="请选择"
                  class="select_input"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in customer_vender"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="车牌号">
                <el-input v-model="searchForm.vehicle_num" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="磅单号">
                <el-input
                  v-model="searchForm.gross_device_num"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button
                class="fr"
                size="small"
                icon="el-icon-search"
                type="primary"
                @click="fetchPageData"
                >搜索</el-button
              >
              <el-button
                class="fr"
                size="small"
                icon="el-icon-refresh"
                @click="clearSearchForm"
                >重置</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="tableContent fl">
      <el-table v-loading="loading" :data="data" border>
        <el-table-column
          prop="vehicle_num"
          label="车牌号"
          width="120"
          fixed="left"
        >
          <template slot-scope="scope">
            <span :class="scope.row.status < -1 ? 'errorText' : ''">
              {{ scope.row.vehicle_num }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="payload"
          label="货物名称"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="gross_weight"
          label="毛重(吨)"
          width="90"
        ></el-table-column>
        <el-table-column
          prop="tare_weight"
          label="皮重(吨)"
          width="90"
        ></el-table-column>
        <el-table-column
          prop="net_weight"
          label="净重(吨)"
          width="90"
        ></el-table-column>
        <el-table-column
          prop="vender"
          label="发货单位"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="customer"
          label="收货单位"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="进场时间" width="120">
          <template slot-scope="scope">
            <div>
              {{
                !scope.row.create_time || scope.row.create_time == ""
                  ? ""
                  : scope.row.create_time.split(" ")[0]
              }}
            </div>
            <div>
              {{
                !scope.row.create_time || scope.row.create_time == ""
                  ? ""
                  : scope.row.create_time.split(" ")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="离场时间" width="120">
          <template slot-scope="scope">
            <div>
              {{
                !scope.row.leave_time || scope.row.leave_time == ""
                  ? ""
                  : scope.row.leave_time.split(" ")[0]
              }}
            </div>
            <div>
              {{
                !scope.row.leave_time || scope.row.leave_time == ""
                  ? ""
                  : scope.row.leave_time.split(" ")[1]
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="70">
          <template slot-scope="scope">
            <el-button type="text" @click="reClickPrint(scope.row)" v-if="false"
              >打印</el-button
            >
            <el-button type="text" @click="reviewCar(scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getRequest, getQueryData, getLoadmeter } from "@/api/public";
export default {
  name: "table-drawer",
  props: {
    // type: {
    //   // error-异常车辆 recent-近期出场车辆
    //   // 只有近期出场有分页表格-废弃
    //   type: String,
    //   default: ""
    // }
    goodSpec: {
      type: Array,
      default: () => {
        return [];
      }
    },
    proLines: {
      type: Array,
      default: () => {
        return [];
      }
    },
    customers: {
      type: Array,
      default: () => {
        return [];
      }
    },
    suppliers: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      data: [],
      searchStr: "",
      searchTime: "",
      showDetailSearch: false,
      searchForm: {
        vehicle_num: "",
        vender: "",
        customer: "",
        payload: "",
        gross_device_num: ""
      },
      loading: false,
      totalNetWeight: 0,
      total: 0,
      page: 1 //第几页
    };
  },
  watch: {
    searchStr: {
      handler() {
        // 展开详细搜索的时候会清空简略搜索的内容
        // 添加条件防止触发多次的搜索
        if (this.searchStr == "" && !this.showDetailSearch) {
          this.fetchPageData();
        }
      }
    }
  },
  computed: {
    customer_vender() {
      let arr = this.customers.concat(this.suppliers);
      let newArr = arr.filter(
        (item, index, self) =>
          self.findIndex(v => v.name === item.name) === index
      );

      return newArr;
    }
  },
  created() {
    this.fetchPageData();
  },
  beforeDestroy() {},
  mounted() {},
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      this.fetchPageData();
    },
    // 点击搜索按钮/日期选择器改变
    handleSearch() {
      this.page = 1;
      this.fetchPageData();
    },
    // 点击显示隐藏详细搜索条件按钮
    handleDetailSearch() {
      this.showDetailSearch = !this.showDetailSearch;
    },
    // 异常/近期出场车辆的详细表格（分页）
    fetchPageData() {
      this.loading = true;
      let querys = [
        {
          property: "F_status",
          value: "-1",
          group: "s",
          operation: "EQUAL",
          relation: "AND"
        }
      ];

      // 判断是简略搜索还是详细搜索
      if (this.showDetailSearch) {
        // 详细搜索
        // 清空简略搜索的内容
        this.searchStr = "";
        if (this.searchForm.payload) {
          querys = [
            ...querys,
            {
              property: "F_payload",
              value: this.searchForm.payload,
              group: "main",
              operation: "LIKE",
              relation: "AND"
            }
          ];
        }
        if (this.searchForm.vehicle_num) {
          querys = [
            ...querys,
            {
              property: "F_vehicle_num",
              value: this.searchForm.vehicle_num,
              group: "main",
              operation: "LIKE",
              relation: "AND"
            }
          ];
        }
        if (this.searchForm.vender) {
          querys = [
            ...querys,
            {
              property: "F_vender",
              value: this.searchForm.vender,
              group: "main",
              operation: "LIKE",
              relation: "AND"
            }
          ];
        }
        if (this.searchForm.customer) {
          querys = [
            ...querys,
            {
              property: "F_customer",
              value: this.searchForm.customer,
              group: "main",
              operation: "LIKE",
              relation: "AND"
            }
          ];
        }
        if (this.searchForm.gross_device_num) {
          querys = [
            ...querys,
            {
              property: "F_gross_device_num",
              value: this.searchForm.gross_device_num,
              group: "main",
              operation: "LIKE",
              relation: "AND"
            }
          ];
        }
      } else {
        // 清空详细搜索的内容
        this.searchForm = {
          vehicle_num: "",
          vender: "",
          customer: "",
          payload: "",
          gross_device_num: ""
        };
        if (this.searchStr) {
          // 简略搜索
          querys = [
            {
              property: "F_payload",
              value: this.searchStr,
              group: "main",
              operation: "LIKE",
              relation: "OR"
            },
            {
              property: "F_vehicle_num",
              value: this.searchStr,
              group: "main",
              operation: "LIKE",
              relation: "OR"
            },
            {
              property: "F_vender",
              value: this.searchStr,
              group: "main",
              operation: "LIKE",
              relation: "OR"
            },
            {
              property: "F_customer",
              value: this.searchStr,
              group: "main",
              operation: "LIKE",
              relation: "OR"
            },
            {
              property: "F_gross_device_num",
              value: this.searchStr,
              group: "main",
              operation: "LIKE",
              relation: "OR"
            }
          ];
        }
      }
      if (this.searchTime && this.searchTime != "") {
        querys = [
          ...querys,
          {
            property: "F_gross_time",
            value:
              [`${this.searchTime} 00:00:00`, `${this.searchTime} 23:59:59`] ||
              [],
            group: "t",
            operation: "BETWEEN",
            relation: "AND"
          }
        ];
      }

      // 获取分页数据
      getQueryData({
        alias: "loadometer",
        queryFilter: {
          pageBean: {
            page: this.page,
            pageSize: 10,
            showTotal: "true"
          },
          querys,
          sorter: [{ property: "leave_time", direction: "DESC" }]
        }
      }).then(response => {
        this.data = response.value || [];
        this.loading = false;
        this.total = response.total || 0;
        this.page = response.page || 1;
      });

      // 获取净重总和
      getQueryData({
        alias: "loadometer",
        queryFilter: {
          pageBean: {
            showTotal: false
          },
          querys,
          sums: ["net_weight"]
        }
      }).then(response => {
        if (response.value[0])
          this.totalNetWeight = response.value[0].net_weight_sum || 0;
      });
    },
    clearSearchForm() {
      // 清空所有的详细搜索条件 页面重置回第一页
      this.searchForm = {
        vehicle_num: "",
        vender: "",
        customer: "",
        payload: "",
        gross_device_num: ""
      };
      this.page = 1;
      this.fetchPageData();
    },
    reClickPrint(item) {
      this.$emit("reClickPrint", item);
    },
    reviewCar(item) {
      this.$emit("reviewCar", item);
    }
  }
};
</script>

<style lang="scss" scoped>
.drawerContent {
  height: 100%;
  width: 100%;
  .searchContent {
    padding: 0 20px;
    margin-bottom: 10px;
    width: 100%;
    line-height: 40px;
    .searchSimple {
      width: 100%;
      .el-input-group {
        width: 350px;
      }
    }
    .searchDetail {
      width: 100%;
      margin-top: 20px;
    }
  }
  .tableContent {
    height: calc(100% - 60px);
    width: 100%;
    padding: 0 20px;
    overflow-y: auto;
    .el-table {
      font-size: 18px;
      margin: 10px 0;
    }
  }
  .errorText {
    position: relative;

    &::after {
      content: "!";
      position: absolute;
      right: -20px;
      top: 0px;
      display: inline-block;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      text-align: center;
      line-height: 14px;
      font-size: 12px;
      font-weight: bold;
      background: #fa5858;
      color: #fff;
      border: 1px solid #fff;
      // box-shadow: 0px 0px 3px 0px #000000;
    }
  }
}
::v-deep {
  input {
    height: 40px;
    padding-left: 0px;
    padding-right: 10px !important;
  }
  .cell.el-tooltip {
    white-space: nowrap;
  }
  .searchDetail {
    .el-form-item {
      margin-bottom: 5px;
      .el-form-item__label {
        font-weight: normal;
        font-size: 18px;
      }
      .el-form-item__content {
        line-height: 32px;
        input {
          height: 32px;
        }
      }
    }
    .select_input {
      width: 100%;
      .el-select__caret {
        font-size: 16px !important;
      }
    }
    .el-button {
      margin-top: 8px;
      margin-left: 10px;
    }
  }
}
</style>
